package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.quality.entity.InspectionItem;
import com.mxpio.erp.common.quality.entity.InspectionItemParam;
import com.mxpio.erp.common.quality.service.InspectionItemParamService;
import com.mxpio.erp.common.quality.service.InspectionItemService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InspectionItemController", description = "质检项目接口")
@RestController
@RequestMapping("/erp/quality/ii/")
public class InspectionItemController {
	
	@Autowired
	private InspectionItemService inspectionItemService;
	@Autowired
	private InspectionItemParamService paramService;

	@GetMapping("list")
	@Operation(summary = "质检项目列表", description = "获取质检项目列表", method = "GET")
	public Result<List<InspectionItem>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<InspectionItem> inspectionItems = inspectionItemService.list(InspectionItem.class, criteria);
		return Result.OK(inspectionItems);
	}
	
	@GetMapping("page")
	@Operation(summary = "质检项目列表", description = "获取质检项目列表", method = "GET")
	public Result<Page<InspectionItem>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<InspectionItem> page = inspectionItemService.listPage(InspectionItem.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{code}")
	@Operation(summary = "根据code获取质检项目", description = "根据code获取质检项目", method = "GET")
	public Result<InspectionItem> getById(@PathVariable(name = "code", required = true) String code) {
		InspectionItem inspectionItem = inspectionItemService.getById(InspectionItem.class, code);
		return Result.OK(inspectionItem);
	}
	
	@PostMapping("add")
	@Operation(summary = "新增质检项目", description = "新增质检项目", method = "POST")
	public Result<InspectionItem> add(@RequestBody InspectionItem inspectionItem) {
		inspectionItemService.save(inspectionItem);
		return Result.OK(inspectionItem);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑质检项目", description = "编辑质检项目", method = "PUT")
	public Result<InspectionItem> edit(@RequestBody InspectionItem inspectionItem) {
		inspectionItemService.update(inspectionItem);
		return Result.OK(inspectionItem);
	}

	@RequestMapping("save")
	@Operation(summary = "保存质检项目", description = "保存质检项目")
	public Result<InspectionItem> save(@RequestBody InspectionItem item) {
		inspectionItemService.save(item,new SmartCrudPolicyAdapter(){
			@Override
			public boolean beforeInsert(CrudContext context) {
				Object o = context.getEntity();
				if(o instanceof InspectionItemParam){
					InspectionItem parent = context.getParent();
					((InspectionItemParam) o).setCode(parent.getCode());
				}
				return true;
			}
		});
		return Result.OK(item);
	}
	@DeleteMapping("remove/{codes}")
	@Operation(summary = "删除质检项目", description = "删除质检项目", method = "DELETE")
	public Result<InspectionItem> remove(@PathVariable(name = "codes", required = true) String codes) {
		String strings[] = codes.split(",");
		for(String key : strings){
			Criteria c = Criteria.create().addCriterion("code", Operator.EQ, key);
			paramService.removeBatch(InspectionItemParam.class, c);//删除子项
			inspectionItemService.delete(InspectionItem.class, key);
		}
		return Result.OK();
	}

	@PutMapping("submit/{code}")
	@Operation(summary = "提交质检项目", description = "提交质检项目", method = "PUT")
	public Result<InspectionItem> submit(@Parameter(description="质检项目编码") @PathVariable(name = "code", required = true) String code) {
		InspectionItem inspectionItem = inspectionItemService.getById(InspectionItem.class,code);
		if(inspectionItem.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())){
			return inspectionItemService.updateBpmnStatus(inspectionItem, OrderStatusEnums.BpmnStatus.AUDITING);
		}else {
			return Result.error("非开立状态不可提交");
		}
	}

	@PutMapping("audit/{code}")
	@Operation(summary = "审核质检项目", description = "审核质检项目", method = "PUT")
	public Result<InspectionItem> audit(@Parameter(description = "质检项目编码") @PathVariable(name = "code", required = true) String code) {
		InspectionItem po = inspectionItemService.getById(InspectionItem.class, code);
		if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) {
			return inspectionItemService.updateBpmnStatus(po, OrderStatusEnums.BpmnStatus.AUDITED);
		} else {
			return Result.error("非审核中状态不可审核");
		}
	}

	@PutMapping("abandon/{code}")
	@Operation(summary = "弃审质检项目", description = "弃审质检项目", method = "PUT")
	public Result<InspectionItem> abandon(@Parameter(description = "质检项目编码") @PathVariable(name = "code", required = true) String code) {
		InspectionItem po = inspectionItemService.getById(InspectionItem.class, code);
		if (po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()) || po.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())) {
			return inspectionItemService.updateBpmnStatus(po, OrderStatusEnums.BpmnStatus.CREATE);
		} else {
			return Result.error("非审核中、已审核状态不可审核");
		}
	}
	
}
